<template>
  <div>
    <el-row>
      <el-col>
        <el-form :model="pageParams" ref="queryForm" size="small" :inline="true" label-width="68px">
          <el-form-item label="发起人" prop="startUserName">
            <el-input
              v-model="pageParams.startUserName"
              placeholder="请输入任务发起人"
              clearable
              style="width: 240px"
            />
          </el-form-item>
          <el-form-item label="流程名称" prop="processDefinitionName">
            <el-input
              v-model="pageParams.processDefinitionName"
              placeholder="请输入流程名称"
              clearable
              style="width: 240px"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="list">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-table :data="formData" style="width: 100%" height="500">
          <el-table-column fixed label="序号" style="width: 100%">
            <template slot-scope="scope">
              <span>{{ (pageParams.pageNum - 1) * pageParams.pageSize + scope.$index + 1}}</span>
            </template>
          </el-table-column>
          <el-table-column
            fixed
            label="发起人"
            prop="startUserName"
            idth="150"
          >
          </el-table-column>
          <el-table-column
            fixed
            label="任务id"
            prop="id"
            width="150">
          </el-table-column>
          <el-table-column
            label="执行id"
            prop="executionId"
            width="150">
          </el-table-column>
          <el-table-column
            label="任务名称"
            prop="name"
            width="150">
          </el-table-column>
          <el-table-column
            label="流程名称"
            prop="processDefinitionName"
            width="150">
          </el-table-column>
          <el-table-column
            label="描述"
            prop="description"
            width="150">
          </el-table-column>
          <el-table-column
            label="负责人名称"
            prop="assigneeName"
            width="150">
          </el-table-column>
          <el-table-column
            label="任务key"
            prop="taskDefinitionKey"
            width="150">
          </el-table-column>
          <el-table-column
            fixed="right"
            label="开始时间"
            prop="startTime"
            :formatter="formatDate"
            width="150">
          </el-table-column>
          <el-table-column
            fixed="right"
            label="结束时间"
            prop="endTime"
            :formatter="formatDate"
            width="150">
          </el-table-column>
          <el-table-column
            fixed="right"
            label="耗费时间(ms)"
            prop="durationInMillis"
            width="120">
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            width="120">
            <template slot-scope="scope">
              <el-button
                @click="show(scope.row)"
                type="text"
                size="small">
                查看
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          @prev-click="prev_click"
          @next-click="next_click"
          @size-change="size_change"
          @current-change="handleCurrentChange"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageParams.pageSize"
          :total="pageParams.total">
        </el-pagination>
      </el-col>
    </el-row>
    <el-dialog :title="approve.title" :visible.sync="approve.open" width="600px" append-to-body>
      <el-form ref="formData" :model="leaveForm" :disabled="approve.disable" size="medium" label-width="100px">
        <el-form-item label="用户">
          <el-input v-model="leaveForm.username" placeholder="请输入用户" readonly :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="开始时间">
          <el-date-picker v-model="leaveForm.startTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                          :style="{width: '100%'}" placeholder="请选择开始时间" clearable></el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间">
          <el-date-picker v-model="leaveForm.endTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                          :style="{width: '100%'}" placeholder="请选择结束时间" clearable></el-date-picker>
        </el-form-item>
        <el-form-item label="请假原因">
          <el-input v-model="leaveForm.reason" type="textarea" placeholder="请输入请假原因"
                    :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
        </el-form-item>

      </el-form>
    </el-dialog>
  </div>
</template>

<script>

import {getCompletedTaskWithMe} from '@/api/bpmn/activiti'
import {formatDate} from '@/api/bpmn/utils'
import {getFormById} from '@/api/bpmn/leave'

export default {
  name: '操作记录',
  data() {
    return {
      formData: [],
      pageParams: {
        pageNum: 1,
        pageSize: 10,
        total: undefined,
        startUserName: undefined,
        processDefinitionName: undefined,
      },
      approve: {
        open: false,
        title: '审批',
        disable: true
      },
      leaveForm: {
        username: undefined,
        userId: undefined,
        startTime: null,
        endTime: null,
        reason: undefined,
      },
    }
  },
  created() {
    this.list()
  },
  methods: {
    list() {
      getCompletedTaskWithMe(this.pageParams)
        .then(res => {
          this.formData = res.rows
          this.pageParams.total = res.total
        })
    },
    formatDate(row, column, cellValue) {
      return formatDate(row, column, cellValue)
    },
    show(obj) {
      // 获取表单信息
      getFormById(obj.businessKey).then(res => {
        this.leaveForm = res
        this.approve.open = true
      })
    },
    // 上一页
    prev_click() {
      this.pageParams.pageNum = this.pageParams.pageNum - 1
      this.list()
    },
    next_click() {
      this.pageParams.pageNum = this.pageParams.pageNum + 1
      this.list()
    },
    size_change(val) {
      this.pageParams.pageSize = val
      this.list()
    },
    handleCurrentChange(val){
      this.pageParams.pageNum = val
      this.list()
    },
    resetQuery() {
      this.pageParams = {
        pageNum: 1,
        pageSize: 10,
        total: undefined,
        startUserName: undefined,
        processDefinitionName: undefined,
      }
      this.list()
    }
  }
}

</script>
